#if !defined(TSTD_H_INCLUDED)
#define TSTD_H_INCLUDED

#pragma once 

#pragma warning(push) 

#include <yvals.h>				// warning numbers get enabled in yvals.h

#pragma warning(disable: 4018)
#pragma warning(disable: 4100)
#pragma warning(disable: 4146)
#pragma warning(disable: 4244)
#pragma warning(disable: 4245)
#pragma warning(disable: 4511)
#pragma warning(disable: 4512)
#pragma warning(disable: 4663)
#pragma warning(disable: 4710)
#pragma warning(disable: 4786)

#include <fstream> 
#include <sstream> 
#include <locale> 
#include <string> 
#include <iomanip>
  
namespace std 
{	// extend the std namespace 

	////////////////////////////////////// 
	// TEMPLATE CLASS basic_wfilebuf_ex 
	// 
	//      With possibilty to open C++ filestreams 
	//      with Unicode filenames 
	// 
	template<class _E, class _Tr = char_traits<_E> > 
		class basic_filebuf_ex : public basic_filebuf<_E, _Tr>  
	{ 
public: 
	typedef basic_filebuf_ex<_E, _Tr> _Myt; 
	
	_Myt* open(const wchar_t *_S, ios_base::openmode _M) 
	{ 
		_Filet *_Fp; 
		if (is_open() || (_Fp = __wFiopen(_S, _M)) == 0) 
			return (0); 
		_Init(_Fp, _Openfl); 
		_Initcvt(); 
		return (this);  
	} 
	
	_Myt* open(const wchar_t *_N, ios::open_mode _M) 
	{ 
		return (open(_N, (ios::openmode)_M));  
	} 
	
	FILE* __wFiopen(const wchar_t* name, ios_base::openmode mode) 
	{ 
		// open a file 
		static const wchar_t *mods[] = 
		{ 
			L"r",  L"w",  L"w",  L"a",  L"rb",  L"wb",  L"wb",  
				L"ab", L"r+", L"w+", L"a+", L"r+b", L"w+b", L"a+b",  
				0 
		}; 
		
		static const int valid[] = 
		{ 
			ios_base::in, 
				ios_base::out, 
				ios_base::out | ios_base::trunc, 
				ios_base::out | ios_base::app, 
				ios_base::in  | ios_base::binary, 
				ios_base::out | ios_base::binary, 
				ios_base::out | ios_base::trunc | ios_base::binary, 
				ios_base::out | ios_base::app   | ios_base::binary, 
				ios_base::in  | ios_base::out, 
				ios_base::in  | ios_base::out   | ios_base::trunc, 
				ios_base::in  | ios_base::out   | ios_base::app, 
				ios_base::in  | ios_base::out   | ios_base::binary, 
				ios_base::in  | ios_base::out   | ios_base::trunc | ios_base::binary, 
				ios_base::in  | ios_base::out   | ios_base::app   | ios_base::binary, 
				0 
		}; 
		
		FILE *fp; 
		int n; 
		ios_base::openmode atefl = mode & ios_base::ate; 
		mode &= ~ios_base::ate; 
		for (n = 0; valid[n] != 0 && valid[n] != mode; ++n) 
			; 
		if (valid[n] == 0 || (fp = ::_wfopen(name, mods[n])) == 0) 
			return (0); 
		if (!atefl || ::fseek(fp, 0, SEEK_END) == 0) 
			return (fp); 
		::fclose(fp); // can't position at end 
		return (0); 
	} 
	}; 
	
	////////////////////////////////////// 
	// TEMPLATE CLASS basic_ifstream_ex 
	// 
	template<class _E, class _Tr = char_traits<_E> > 
		class basic_ifstream_ex : public basic_istream<_E, _Tr>  
	{ 
public: 
	typedef basic_ifstream_ex<_E, _Tr> _Myt; 
	typedef basic_filebuf_ex<_E, _Tr> _Myfb; 
	
	basic_ifstream_ex() 
		: basic_istream<_E, _Tr>(&_Fb) 
	{ 
	} 
	
	explicit basic_ifstream_ex(const wchar_t *_S, ios_base::openmode _M = in) 
		: basic_istream<_E, _Tr>(&_Fb) 
	{ 
		if (_Fb.open(_S, _M | in) == 0) 
			setstate(failbit);  
	} 
	
	virtual ~basic_ifstream_ex() 
	{ 
	} 
	
	_Myfb *rdbuf() const 
	{  
		return ((_Myfb *)&_Fb);  
	} 
	
	bool is_open() const 
	{  
		return (_Fb.is_open());  
	} 
	
	void open(const wchar_t *_S, ios_base::openmode _M = in) 
	{ 
		if (_Fb.open(_S, _M | in) == 0) 
			setstate(failbit);  
	} 
	
	void open(const wchar_t *_S, ios_base::open_mode _M) 
	{ 
		open(_S, (openmode)_M);  
	} 
	
	void close() 
	{  
		if (_Fb.close() == 0) 
			setstate(failbit);  
	} 
	
private: 
	_Myfb _Fb; 
	}; 
	
	///////////////////////////////////// 
	// TEMPLATE CLASS basic_ofstream_ex 
	// 
	template<class _E, class _Tr = char_traits<_E> > 
		class basic_ofstream_ex : public basic_ostream<_E, _Tr>  
	{ 
public: 
	typedef basic_ofstream_ex<_E, _Tr> _Myt; 
	typedef basic_filebuf_ex<_E, _Tr> _Myfb; 
	
	basic_ofstream_ex() 
		: basic_ostream<_E, _Tr>(&_Fb) 
	{ 
	} 
	
	explicit basic_ofstream_ex(const wchar_t *_S,	ios_base::openmode _M = 
		out | trunc) 
		: basic_ostream<_E, _Tr>(&_Fb) 
	{ 
		if (_Fb.open(_S, _M | out) == 0) 
			setstate(failbit);  
	} 
	
	virtual ~basic_ofstream_ex() 
	{ 
	} 
	
	_Myfb *rdbuf() const 
	{ 
		return ((_Myfb *)&_Fb);  
	} 
	
	bool is_open() const 
	{ 
		return (_Fb.is_open());  
	} 
	
	void open(const wchar_t *_S, ios_base::openmode _M = out | trunc) 
	{ 
		if (_Fb.open(_S, _M | out) == 0) 
			setstate(failbit);  
	} 
	
	void open(const wchar_t *_S, ios_base::open_mode _M) 
	{ 
		open(_S, (openmode)_M);  
	} 
	
	void close() 
	{ 
		if (_Fb.close() == 0) 
			setstate(failbit);  
	} 
	
private: 
	_Myfb _Fb; 
	}; 
	
	///////////////////////////////////// 
	// TEMPLATE CLASS basic_fstream_ex 
	// 
	template<class _E, class _Tr = char_traits<_E> > 
		class basic_fstream_ex : public basic_iostream<_E, _Tr>  
	{ 
public: 
	basic_fstream_ex() 
		: basic_iostream<_E, _Tr>(&_Fb) 
	{ 
	} 
	
	explicit basic_fstream_ex(const wchar_t *_S, ios_base::openmode _M = in | out) 
		: basic_iostream<_E, _Tr>(&_Fb) 
	{ 
		if (_Fb.open(_S, _M) == 0) 
			setstate(failbit);  
	} 
	
	virtual ~basic_fstream_ex() 
	{ 
	} 
	
	basic_filebuf_ex<_E, _Tr> *rdbuf() const 
	{  
		return ((basic_filebuf_ex<_E, _Tr> *)&_Fb);  
	} 
	
	bool is_open() const 
	{  
		return (_Fb.is_open());  
	} 
	
	void open(const wchar_t *_S, ios_base::openmode _M = in | out) 
	{ 
		if (_Fb.open(_S, _M) == 0) 
			setstate(failbit);  
	} 
	
	void open(const wchar_t *_S, ios_base::open_mode _M) 
	{ 
		open(_S, (openmode)_M);  
	} 
	
	void close() 
	{ 
		if (_Fb.close() == 0) 
			setstate(failbit);  
	} 
	
private: 
	basic_filebuf_ex<_E, _Tr> _Fb; 
	}; 

	//////////////////////////////////////////////////////////////////////////////////
	// generic typedefs
	//////////////////////////////////////////////////////////////////////////////////
	typedef std::basic_string<TCHAR>		tstring;
	
	typedef std::basic_istream<TCHAR, char_traits<TCHAR> >		tistream;
	typedef std::basic_ostream<TCHAR, char_traits<TCHAR> >		tostream;
	
	typedef std::basic_filebuf<TCHAR, char_traits<TCHAR> >		tfilebuf;
	typedef std::basic_streambuf<TCHAR, char_traits<TCHAR> >	tstreambuf;
	
	typedef std::basic_istringstream<TCHAR>	tistringstream;
	typedef std::basic_ostringstream<TCHAR>	tostringstream;
	
	typedef std::basic_stringbuf<TCHAR>		tstringbuf;
	typedef std::basic_stringstream<TCHAR>	tstringstream;
	
#ifdef _UNICODE 
	typedef basic_ifstream_ex<char, char_traits<char> >			ifstream_ex;
	typedef basic_ofstream_ex<char, char_traits<char> >			ofstream_ex;
	typedef basic_fstream_ex<char, char_traits<char> >			fstream_ex;
	typedef basic_ifstream_ex<wchar_t, char_traits<wchar_t> >	wifstream_ex;
	typedef basic_ofstream_ex<wchar_t, char_traits<wchar_t> >	wofstream_ex;
	typedef basic_fstream_ex<wchar_t, char_traits<wchar_t> >	wfstream_ex;
	
	typedef std::wifstream_ex		tifstream;
	typedef std::wofstream_ex		tofstream;
	typedef std::wfstream_ex		tfstream;
#else 
	typedef std::ifstream		ifstream_ex; 
	typedef std::ofstream		ofstream_ex; 
	typedef std::fstream		fstream_ex; 
	typedef std::wifstream		wifstream_ex; 
	typedef std::wofstream		wofstream_ex; 
	typedef std::wfstream		wfstream_ex; 
	
	typedef std::ifstream_ex	tifstream;
	typedef std::ofstream_ex	tofstream;
	typedef std::fstream_ex		tfstream;
#endif	// #ifdef _UNICODE

}// namespace std 

#pragma warning(pop)

#endif	// #if !defined(TSTD_H_INCLUDED)